//
//  SubjectViewController.m
//  ZhongWangYuan
//
//  Created by ZJ on 2018/6/2.
//  Copyright © 2018年 ZJ. All rights reserved.
//

#import "SubjectViewController.h"
#import "LPZWYCommonSpecilTableViewCell.h"
#import "LPZWYCommonClassTableViewCell.h"
#import "ZJVidoViewTabViewController.h"
#define spicalCell @"LPZWYCommonSpecilTableViewCell"
#define classCell @"LPZWYCommonClassTableViewCell"
@interface SubjectViewController ()<UITableViewDelegate,UITableViewDataSource>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
@property (nonatomic , copy) NSMutableArray * dataArray;
/**    */
@property (strong, nonatomic) NSMutableArray  *orangalArray;

/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

@end

@implementation SubjectViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [self.tableView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticeObject:) name:NOTI_UPDATE_TAB object:nil];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        [weakSelf netWorking_header];
        
    }];
    
     self.leftButton.hidden = YES;
    if (self.subTitle.length != 0) {
        self.leftButton.hidden = NO;
        self.titlelabel.text = self.subTitle;
    }
   
   
    [self layoutUI];
}

#pragma mark - 通知
- (void)noticeObject:(NSNotification *)noti{
    NSDictionary *dic = noti.object;
    if ([[dic objectForKey:@"index"] integerValue] == 2) {
        [self.tableView.mj_header beginRefreshing];
    }
}
#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    [self.orangalArray removeAllObjects];
    __weak typeof(self) weakSelf = self;
    [LPRequestManager request_getMyClassAndCoursewareWithsuccessBlock:^(id object) {
        NSString *code = [object objectForKey:@"code"];
        if ([code integerValue] == 1) {
             NSArray *classlist = [[[object objectForKey:@"data"] objectForKey:@"classlist"] objectForKey:@"data"];
             NSArray *coursewarelist = [[[object objectForKey:@"data"] objectForKey:@"coursewarelist"] objectForKey:@"data"];
           
            
            if (coursewarelist.count == 0 && classlist.count == 0) {
                [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                
            }else{
                
                NSArray *clsModelList = [NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:classlist];
                for (LPZWYChooseClassClassModel *class in clsModelList) {
                    class.classOrCourse = @"1";
                }
                [weakSelf.orangalArray addObjectsFromArray:clsModelList];
                
                
                
                NSArray *couModelList = [NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:coursewarelist];
                for (LPZWYChooseClassClassModel *class in couModelList) {
                    class.classOrCourse = @"2";
                }
                [weakSelf.orangalArray addObjectsFromArray:couModelList];
                
                self.dataArray = self.orangalArray;
                
                [weakSelf.tableView reloadData];
            }
            
        }else{
            [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
        }
        [weakSelf.tableView.mj_header endRefreshing];
        
    } failBlock:^(id object) {
        [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    if (self.subTitle.length != 0) {
        self.tableView.sd_layout
        .topSpaceToView(self.navigView, 0)
        .leftEqualToView(self.view)
        .rightEqualToView(self.view)
        .bottomEqualToView(self.view);
    }else{
        self.tableView.sd_layout
        .leftSpaceToView(self.view , 0)
        .rightSpaceToView(self.view , 0)
        .topSpaceToView(self.navigView , 0)
        .bottomSpaceToView(self.view , LP_TabBarHeight);
    }
   
}


#pragma mark - 私有方法


#pragma mark - 监听事件



#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
    
    if ([model.classOrCourse integerValue] == 1) {
        LPZWYCommonSpecilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:spicalCell forIndexPath:indexPath];
        cell.model = model;
        cell.isShowEnd = 0;
        return cell;
    }else{
        LPZWYCommonClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:classCell forIndexPath:indexPath];
        cell.isMintue = YES;
        cell.model = model;
        return cell;
    }
    
}


/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
        ZJVidoViewTabViewController *vc = [ZJVidoViewTabViewController new];
        vc.model = model;
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
}


#pragma mark - 侧滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
    if ([model.classOrCourse integerValue] == 2) {
        return YES;
    }
    return NO;
    
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
    
}


- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return NO;
    
}



/**  实现了删除操作  */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
     LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
    [YJProgressHUD showMessage:@"删除中..." duration:10 view:self.view];
    [LPRequestManager request_deleteToBeStudiedUseWeicatAppWithcoursewareid:model.coursewareid successBlock:^(id object) {
        [YJProgressHUD hide];
        NSString *code   = [object objectForKey:@"code"];
        if ([code integerValue] == 1) {
            NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
            if ([result integerValue] == 1) {
                [self.dataArray removeObjectAtIndex:indexPath.row];
                [self.tableView reloadData];
            }else{
                [YJProgressHUD showSuccess:[[object objectForKey:@"data"] objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
            }
            
        }
    } failBlock:^(id object) {
         [YJProgressHUD hide];
    }];
    
    
}



#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight - LP_TabBarHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
        [_tableView registerClass:[LPZWYCommonClassTableViewCell class] forCellReuseIdentifier:classCell];
        [_tableView registerClass:[LPZWYCommonSpecilTableViewCell class] forCellReuseIdentifier:spicalCell];
         [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)orangalArray{
    if (!_orangalArray) {
        _orangalArray = [NSMutableArray array];
    }
    return _orangalArray;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
